package com.kfpanda.file.monitor.listener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Timer;

import net.contentobjects.jnotify.JNotifyListener;

import org.springframework.beans.factory.annotation.Value;

public class FileListener implements JNotifyListener {
	
	@Value("${name:}")
	private String path;
	private FileWriter fileWriter;
	private Timer timer = new Timer();
	private TodayTimerTask todayTask = new TodayTimerTask();
	
	FileListener(){
		try {
			fileWriter = new FileWriter(this.path + todayTask.getToday());
		} catch (IOException e) {
			e.printStackTrace();
		}
		timer.schedule(todayTask, 1000);
	}
	
	protected void wirteLine(String line){
		try {
			this.fileWriter.write(line + "\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
		String line = "renamed" + "  |  " + rootPath + "  |  " + oldName + "  |  " + newName;
		this.wirteLine(line);
	}

	public void fileModified(int wd, String rootPath, String name) {
		String line = "modified  |  " + rootPath + "  |  " + name;
		this.wirteLine(line);
	}

	public void fileDeleted(int wd, String rootPath, String name) {
		String line = "deleted  |  " + rootPath + "  |  " + name;
		this.wirteLine(line);
	}

	public void fileCreated(int wd, String rootPath, String name) {
		String line = "created  |  " + rootPath + "  |  " + name;
		this.wirteLine(line);
	}

}
